package com.example.meeting.controller;

import com.example.meeting.model.domain.Person;
import com.example.meeting.model.responseData.CommonResult;
import com.example.meeting.service.IPersonService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
public class PersonController {
    @Autowired
    private IPersonService personService;

    /**
     * 根据姓名查询列席人员
     * @param name
     * @return
     */
    @GetMapping("/person")
    @RequiresAuthentication
    public CommonResult selectPersonByName(@RequestParam String name) {
        Person person = personService.selectPersonByName(name);
        if (person == null) return CommonResult.success(null);
        else return CommonResult.success(person);
    }

    @PostMapping("/personList")
    @RequiresAuthentication
    public CommonResult selectPersonList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                       @RequestBody Person person) {
        PageInfo<Person> personList = personService.selectPersonList(page,pageSize, person);
        if (personList == null) {
            return CommonResult.fail("查询失败，没有任何列席人员信息");
        }
        return CommonResult.success(personList);
    }

    /**
     * 新增列席人员
     * @param person
     * @return
     */
    @PostMapping("/person")
    @RequiresAuthentication
    public CommonResult addPerson(@Validated @RequestBody Person person) {
        int res = personService.addPerson(person);
        if (res == 0) {
            return CommonResult.fail("新增列席人员失败！");
        }
        return CommonResult.success(person.getId());
    }

    /**
     * 更新列席人员
     * @param person
     * @return
     */
    @PutMapping("/person")
    @RequiresAuthentication
    public CommonResult updatePerson(@Validated @RequestBody Person person) {
        int res = personService.updatePerson(person);
        if (res == 0) {
            return CommonResult.fail("用户信息更新失败");
        }
        return CommonResult.success(res);
    }
}
